<?php
namespace App\Http\Controllers\ShopApi\V1\Common;

use App\Http\Controllers\ShopApiController;
use App\Http\Controllers\Business\Shop\V1\Account\Account;
use App\Http\Controllers\Business\Shop\V1\Auth\ShopAuth;
use App\Http\Controllers\Business\Shop\V1\PublicBusiness;
use Log;

class CommonController extends ShopApiController
{
    //首页
    public function index() {
        $shopUserId = $this->shopUserId;
        $shopId = $this->request->get('shopid');
        $result = PublicBusiness::index($shopId, $shopUserId);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        }
        $data = $result['data'];
        //权限
        $authResult = PublicBusiness::getAuth($shopUserId, $shopId);
        if($authResult['status'] == 'failed') {
            $data['auth'] = [];
        } else {
            $data['auth'] = $authResult['data'];
        }
        $data['week'] = date('w');
        $data['today'] = date("Y-m-d");
        return $this->responseSuccessData($data);
    }
    //获取商家信息
    public function getShop() {
        $shopUserId = $this->shopUserId;
        $result = PublicBusiness::getShop($shopUserId);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        }
        $data = $result['data'];
        return $this->responseSuccessData($data);
    }
    //通过商家id， 商家管理人员id获取权限
    public function getAuth() {
        $shopId = $this->request->get('shopid');
        $shopUserId = $this->shopUserId;
        $result = PublicBusiness::getAuth($shopUserId, $shopId);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        }
        $data = $result['data'];
        return $this->responseSuccessData($data);
    }
    //我的信息
    public function getUserInfo() {
        $shopUserId = $this->shopUserId;
        $result = PublicBusiness::getUserInfo($shopUserId);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        }
        $data = $result['data'];
        return $this->responseSuccessData($data);
    }
    //修改或添加密码
    public function updateUserPassword(){
        $shopUserId = $this->shopUserId;
        $code = $this->request->get('code');
        $password = $this->request->get('password');
        $confirmPassword = $this->request->get('confirmpassword');
        $result = PublicBusiness::updateUserPassword($shopUserId,$code,$password,$confirmPassword);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessMessage($result['message']);
        }
    }

    //银行卡
    public function getBankCard() {
        $shopId =  $this->request->get('shopid');
        $result = Account::getBankCard($shopId);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        }
        return $this->responseSuccessData($result['data']);
    }
    //意见反馈
    public function feedback() {
        $shopUserId = $this->shopUserId;
        $content = $this->request->get('content');
        $osVersion = $this->request->get('osversion');
        $phoneType = $this->request->get('phonetype');
        $versionCode = $this->request->get('versioncode');
        if($this->request->has('shopid')){
            $shopId = $this->request->get('shopid');
        }else{
            $shopId = '';
        }
        $type = 'shop';
        $result = PublicBusiness::feedback($shopUserId, $content, $type, $osVersion, $phoneType, $versionCode,$shopId);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        }
        return $this->responseSuccessMessage($result['message']);
    }
    //andriod 版本更新
    public function androidVersion() {
        if(env('WEB_URL') == 'https://test2.fbw-china.com/') {
            //测试环境
            $data = [
                'versioncode' => 230,
                'versionname' => '2.3.0',
                'isfroce' => 0,
                'updatemsg' => "测试版安装密码是fbw123",
                'downloadurl' => 'https://www.pgyer.com/fbwbusiness_androidcs'
            ];
        } else {
            //正式环境
            $data = [
                'versioncode' => 230,
                'versionname' => '2.3.0',
                'isfroce' => 1,
                'updatemsg' => "1.登录方式新增了密码登录;\n2.地推结款对异常打款做了优化;\n3.商家版的翻倍日历去掉了“设置”",
                'downloadurl' => 'http://file.fbw-china.com/download/fbwbusiness.apk'
            ];
        }
        return $this->responseSuccessData($data);
    }
    public function iOSVersion() {
        if(env('WEB_URL') == 'https://test2.fbw-china.com/') {
            //测试环境
            $data = [
                'versioncode' => 0,
                'versionname' => '230',
                'isfroce' => 1,
                'updatemsg' => "测试版安装密码是fbw123",
                'downloadurl' => 'https://www.pgyer.com/fbwbusiness_ioscs'
            ];
        } else {
            //正式环境
            $data = [
                'versioncode' => 0,
                'versionname' => '230',
                'isfroce' => 1,
                'updatemsg' => "1.登录方式新增了密码登录;\n2.地推结款对异常打款做了优化;\n3.商家版的翻倍日历去掉了“设置”",
                'downloadurl' => 'https://itunes.apple.com/cn/app/id1161229230'
            ];
        }
        return $this->responseSuccessData($data);
    }
}